import orm
import time

def show_bill():
    path = r'cashbook.json'
    in_money = 0
    out_money = 0
    balance = 0
    cash_list = orm.read_data(path)
    print("---------------------------------------------")
    print("时间\t\t\t\t\t\t项目\t\t分类\t\t金额")
    for cash in cash_list:
        print(f"{cash['date']}\t\t{cash['project']}\t\t{cash['sort']}\t\t{cash['amount']}")
        if cash['sort'] == '收入':
            in_money += int(cash['amount'])
        else:
            out_money -= int(cash['amount'])
    balance = balance + in_money - out_money
    print("---------------------------------------------")
    print(f"总支出金额：{out_money}，总收入金额：{in_money}，结余金额：{balance}")

def add_bill():
    path = r'cashbook.json'
    new_date = time.strftime(r'%Y/%m/%d %H:%M:%S')
    new_project = input("请输入账单项目：")
    new_sort = int(input("请输入收支分类（1收入 2支出）："))
    new_amount = input("请输入收支金额：")
    if new_sort == 1:
        new_bill = {"date": new_date, "project": new_project, "sort": "收入", "amount": new_amount}

    else:
        new_bill = {"date": new_date, "project": new_project, "sort": "支出", "amount": '-'+new_amount}

    bill_list = orm.read_data(path)
    bill_list.append(new_bill)
    orm.write_data(bill_list, path)
    print("添加成功")

if __name__ == '__main__':
    # show_bill()
    add_bill()